package com.cx.cxapi.uninstall;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.cx.cxapi.R;

import java.util.ArrayList;

public   class WhitelistUninstallAdapter extends BaseAdapter {
    ArrayList mWhitelistPackages;
    Context mContext;
    int saveposition;
    LayoutInflater layoutInflater;
    private DeleteCallback mCallback;
    private String TAG = WhitelistUninstallAdapter.class.getSimpleName();

    public WhitelistUninstallAdapter(Context mContext) {
        this.mContext =mContext;
        layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mWhitelistPackages.size();
    }

    @Override
    public Object getItem(int position) {
        return mWhitelistPackages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ListItemHoder listItemHoder;
        if (convertView == null){
            listItemHoder = new ListItemHoder();
            convertView =   layoutInflater.inflate(R.layout.uninstall_whitelist_item,null);
            listItemHoder.title = (TextView)convertView.findViewById(R.id.whitelist_item_title);
            listItemHoder.content = (TextView)convertView.findViewById(R.id.whitelist_item_content);
            listItemHoder.delete = (TextView)convertView.findViewById(R.id.whitelist_item_delete);
            convertView.setTag(listItemHoder);
        }else {
            listItemHoder = (ListItemHoder) convertView.getTag();
        }

            String value = (String) mWhitelistPackages.get(position);
            android.util.Log.i(TAG, "getView: mWhitelistPackages "+value + " : position "+position);

            listItemHoder.content.setText(value);


            listItemHoder.delete.setTag(position);//this is delete key

        listItemHoder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String trim = listItemHoder.content.getText().toString().trim();
                Log.i(TAG, "getView: delete "+trim +" : position "+position +" saveposition:"+v.getTag().toString());
                saveposition = Integer.parseInt(v.getTag().toString());// TODO
                mCallback.deletePosition(trim,saveposition);

            }
        });
        return convertView;

    }
    public interface DeleteCallback {
        void deletePosition(String packageName, int saveposition);
    }
    public void setDeleteCallback(DeleteCallback Callback ){
    this.mCallback=Callback;
    }


    public void setData(ArrayList mWhitelistPackages) {
		if(mWhitelistPackages !=null){
			Log.i("MyListViewAdapter", "setData: mWhitelistPackages  "+mWhitelistPackages.size());
		}

        this.mWhitelistPackages=mWhitelistPackages;
    }


    class  ListItemHoder{
        TextView title;
        TextView content;
        TextView delete;
    }

}